package com.bwie.service.impl;

import cn.hutool.core.util.StrUtil;
import com.bwie.common.domain.response.SpotResponse;
import com.bwie.common.result.Result;
import com.bwie.mapper.SpotMapper;
import com.bwie.mapper.UserMapper;
import com.bwie.mq.SpotMq;
import com.bwie.service.SpotService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SpotServiceImpl implements SpotService {

    @Autowired
    private SpotMapper spotMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SpotMq spotMq;

    @Override
    public PageInfo<SpotResponse> list(SpotResponse spot) {

        //从redis中取出来
//        if(spot.getPageNum()==1){
//            if(redisTemplate.hasKey(redisTemplate.opsForValue().get(spot.getPageSize()))){
//                List<SpotResponse> list = (List<SpotResponse>) redisTemplate.opsForValue().get(spot.getPageSize());
//                return new PageInfo<SpotResponse>(list);
//            }else{
//                PageHelper.startPage(spot.getPageNum(),spot.getPageSize());
//                List<SpotResponse> list = spotMapper.list(spot);
//                return new PageInfo<>(list);
//            }
//        }

        PageHelper.startPage(spot.getPageNum(),spot.getPageSize());
        List<SpotResponse> list = spotMapper.list(spot);
        PageInfo<SpotResponse> info = new PageInfo<>(list);
        return info;
    }

    @Override
    public void upd(SpotResponse spot) {
        String spotName = spot.getSpotName();
        Integer spotState = spot.getSpotState();
        Integer spotPrice = spot.getSpotPrice();
        Integer userState = spot.getUserState();
        if(userState==null){
            throw new RuntimeException("用户状态不能为空");
        }
        if(StrUtil.isBlank(spotName)){
            throw new RuntimeException("名称不能为空");
        }
        if(spotState==null){
            throw new RuntimeException("状态不能为空");
        }
        if(spotPrice==null){
            throw new RuntimeException("金额不能为空");
        }
        //门票价格为30元，点击“确认预约”后扣除相应金额，并更新门票状态标识，用于后续礼品兑换控制
        if(spotPrice!=30 && spotState==1 ){
            //把用户余额取出来
            Integer object = (Integer) redisTemplate.opsForValue().get("uid" + spot.getUserMoney());

            if( spotPrice <= 1000){
                //把用户余额减去30
                redisTemplate.opsForValue().decrement("uid" + spot.getUserMoney(),spotPrice);
                //更新状态
                spotMapper.upd(spot);
                spot.setUserState(1);
            }

            //点击“退票”按钮时弹出确认提示框，用户确认后，将已支付金额退还至账户余额（5分）。
            if(spotState==2){
                redisTemplate.opsForValue().increment("uid" + spot.getUserMoney(),spotPrice);
                spotMapper.upd(spot);
            }
        }
        spotMq.send(spot);
    }
}
